//
//  JourneryNetHandler.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneryNetHandler.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
@implementation JourneryNetHandler


+ (void)getDataWithUrl:(NSString *)str completion:(void (^)(NSDictionary *))block
{
    
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    [cookieJar cookiesForURL:[NSURL URLWithString:str]];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
//        NSLog(@"%@", cookie);
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    
//    NSMutableDictionary *properties = [NSMutableDictionary dictionary];
//    [properties setValue:@"1" forKey:NSHTTPCookieValue];
//    [properties setValue:@"has_visited_default_hot" forKey:NSHTTPCookieName];
////    [properties setValue:@".breadtrip.com" forKey:NSHTTPCookieDomain];
////    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
////    [properties setValue:@"259200" forKey:NSHTTPCookieValue];
////    [properties setValue:@"Max-Age" forKey:NSHTTPCookieName];
////    [properties setValue:@"/" forKey:NSHTTPCookiePath];
//    [properties setValue:@"i_adbceb7dd567be98298d498b5f3901756d8e6dddfffc8031a24f567406e0306896ec5238" forKey:NSHTTPCookieValue];
//    [properties setValue:@"bt_devid" forKey:NSHTTPCookieName];
//    [properties setValue:@"1427694984,1427699390,1427709421,1427709519" forKey:NSHTTPCookieValue];
//    [properties setValue:@"Hm_lvt_39ccc5353de4e1969b414e80e516d5a7" forKey:NSHTTPCookieName];
//    [properties setValue:@"f293f4e00cd25f689b4623c083db6635" forKey:NSHTTPCookieValue];
//    [properties setValue:@"sessionid" forKey:NSHTTPCookieName];
//    
//    [properties setObject:@"f293f4e00cd25f689b4623c083db6635" forKey:NSHTTPCookieName];
//    [properties setObject:@"i_adbceb7dd567be98298d498b5f3901756d8e6dddfffc8031a24f567406e0306896ec5238" forKey:NSHTTPCookieValue];
//    [properties setObject:@".breadtrip.com" forKey:NSHTTPCookieDomain];
//    [properties setObject:@".breadtrip.com" forKey:NSHTTPCookieOriginURL];
//    [properties setObject:@"/" forKey:NSHTTPCookiePath];
//    [properties setObject:@"0" forKey:NSHTTPCookieVersion];
//    
//    
//    NSHTTPCookie *cookie =[NSHTTPCookie cookieWithProperties:properties];
////    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:coookie];
//    
////    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//    NSArray *cookies=[NSArray arrayWithObjects:cookie,nil];
//    NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];


//    [manager.requestSerializer setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];

    
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error!!!");
    }];
    
}




#pragma mark cookie 调研

- (void)cookit
{
    //查看cookit
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        NSLog(@"%@", cookie);
    }
}

- (void)cookitUse
{
    
    //Create a cookie
//    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
//    [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
//    [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
//    [properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain];
//    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
//    [properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];
//    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
    
    //This url will return the value of the 'ASIHTTPRequestTestCookie' cookie
//   NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/destination/index_places/7/"];
//    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
////
//    [request setUseCookiePersistence:YES];
//    [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
//    [request startSynchronous];
    NSString *str = @"http://api.breadtrip.com/destination/index_places/7/";
    
    
    AFHTTPRequestOperationManager *manageCookit = [AFHTTPRequestOperationManager manager];
    [manageCookit GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    //Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
//    NSLog(@"%@",[request responseString]);
}

//-(NSMutableArray*)retrunCookies{
//    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
//    [properties setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
//    [properties setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
//    [properties setValue:@"" forKey:NSHTTPCookieDomain];
//    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
//    [properties setValue:@"" forKey:NSHTTPCookiePath];
//    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//    return [NSMutableArray arrayWithObject:cookie];
//}
@end
